layui.define(['layer'], function (exports) {
	var $ = layui.$;
	//直接使用
	if (typeof mui == "undefined") {
		
		//加载app的js css不要动态加载这样样式会有刷一下乱
		var oHead = document.getElementsByTagName('head').item(0);
		var oScript= document.createElement("script");
		oScript.type = "text/javascript";
		oScript.src="/admin/utils/mui/mui.min.js";
		oHead.appendChild(oScript);
	}
	
	var obj = {
		//设置主窗口标题栏文字
		setMainBarTitle: function(json){
			if(json.gradients == 0 ){
				var tabHeadHtml = '<header id="header" class="mui-bar mui-bar-nav">\
					<h1 class="mui-title">'+json.title+'</h1>\
				</header>';
			}else{
				var tabHeadHtml = '<header id="header" class="mui-bar mui-bar-transparent">\
					<h1 class="mui-title">'+json.title+'</h1>\
				</header>';
			}
			$(document.body).append(tabHeadHtml);
			
			//走这里的时候做一个选择便利一下数据
			console.log(window.parent.tabBar);
		},
		//设置导航栏信息
		setBarTitle: function(json){
			$('#header', window.parent.document).html('<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a><h1 class="mui-title">'+json.title+'</h1>\
		<a class="mui-icon mui-icon-bars mui-pull-right"></a></header>');
		
		    //走这里的时候做一个选择便利一下数据
			console.log(window.parent.tabBar);
			
			$(document).ready(function(e) { 
				var counter = 0;
				//成功监听到了后退
				if (window.history && window.history.pushState) {
						 $(window).on('popstate', function () {
								//不可后退了 回到首页了
								window.history.pushState('forward', null, '#');
								window.history.forward(1);
								//重新载入
								window.parent.location.reload();
								//alert("不可回退");
						});
				  }
	
				  window.history.pushState('forward', null, '#'); //在IE中必须得有这两行
				  window.history.forward(1);
			});
		},
		//设置底部选项卡菜单
		setTabBar: function(json){			
			  let tabBarHtml = '<nav class="mui-bar mui-bar-tab">';
			  for(i in json.list){
				  var obj = json.list[i];
				  var className = '';
				  if(i == 0){
					  className = ' mui-active';
				  }
				  var pages = (obj.page).split("/");
				  var page = obj.page +'/'+ pages[pages.length - 1] + '.html';
				  json.list[i]['page'] = page;
				  tabBarHtml += '<a class="mui-tab-item'+className+'" href="'+page+'" onclick="opentabBar(this)">\
						<span class="mui-icon '+obj.icon+'"></span>\
						<span class="mui-tab-label">'+obj.text+'</span>\
					</a>';
			  }
			  window.tabBar = json.list;
			  tabBarHtml += '</nav>';
			  
			  
			  let iframeHeight = $(window).height()-94;
			  var muiContent = '<div class="mui-content"><iframe src="'+json.list[0]['page']+'" id="app-content"  style="height:'+iframeHeight+'px;width: 100%;z-index: 999;" frameborder="0"></iframe></div>';
			  $(document.body).append(muiContent);	
			  
			  window.onresize = function(){
			  	$("#app-content").css('height', ($(window).height()-94)+'px');
			  }
			  
			  $(document.body).append(tabBarHtml);
			  
			  window.opentabBar = function(obj){
				  //改变父页面框架的值
				  console.log(obj);
				  $("#app-content", window.parent.document).attr("src", obj.href);
			  }
		}
	};
	exports('app', obj);//导出
});